package com.example.administrator.fragmentdome3;

import android.app.Fragment;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

public class ListTitleFragment extends ListFragment {
    private static final String TAG = "ListTitleFragment";

    public static final int REQUEST_DETAIL = 0x110;
    public static final int RESULT_DETAIL = 0x111;
    private List<String> mTitle = Arrays.asList("Hello", "World", "Android");
    private int mCurrentPos;
    private ArrayAdapter<String> mAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mTitle);
        setListAdapter(mAdapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        //记录当前点击的位置，用于回到该Activity页面，改变点击条目的数据
        mCurrentPos = position;
        Intent intent = new Intent(getActivity(), ContentActivity.class);
        intent.putExtra(ContentFragment.INTENT_TEXT, mTitle.get(position));
        startActivityForResult(intent, REQUEST_DETAIL);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_DETAIL && data != null) {
            mTitle.set(mCurrentPos, mTitle.get(mCurrentPos) + "---" + data.getStringExtra(ContentFragment.RESPONSE));
            mAdapter.notifyDataSetChanged();
        }
    }
}
